# tshark(1) completion                                     -*- shell-script -*-

_tshark()
{
    local cur prev words cword prefix
    _init_completion -n : || return

    case $cur in
        -o*)
            prefix=-o
            ;;
        -X*)
            prefix=-X
            ;;
    esac

    case ${prefix:-$prev} in
        --*)
            # Fallback to completion of long options below.
            ;;
        -o*)
            if [[ $cur == *:* ]]; then
                cur=${cur#*:}
                _filedir
            else
                [[ -v _tshark_prefs ]] ||
                    _tshark_prefs="$("$1" -G defaultprefs 2>/dev/null | command sed -ne 's/^#\{0,1\}\([a-z0-9_.-]\{1,\}:\).*/\1/p' |
                        tr '\n' ' ')"
                : ${prefix:=}
                COMPREPLY=($(compgen -P "$prefix" -W "$_tshark_prefs" \
                    -- "${cur:${#prefix}}"))
                [[ ${COMPREPLY-} == *: ]] && compopt -o nospace
            fi
            return
            ;;
        -*[fsBDLcRNdCeEzhvoK])
            return
            ;;
        -*i)
            COMPREPLY=($(compgen -W \
                "$("$1" -D 2>/dev/null | awk '{print $2}')" -- "$cur"))
            return
            ;;
        -*y)
            local opts i
            for ((i = ${#words[@]} - 1; i > 0; i--)); do
                if [[ ${words[i]} == -i ]]; then
                    opts+="-i ${words[i + 1]}"
                    break
                fi
            done
            COMPREPLY=($(compgen -W "$("$1" $opts -L 2>/dev/null |
                awk '/^  / { print $1 }')" -- "$cur"))
            return
            ;;
        -*[ab])
            COMPREPLY=($(compgen -W 'duration: filesize: files:' -- "$cur"))
            [[ ${COMPREPLY-} == *: ]] && compopt -o nospace
            return
            ;;
        -*[rH])
            # -r accepts a lot of different file types
            _filedir
            return
            ;;
        -*w)
            _filedir
            [[ $cur == @(|-) ]] && COMPREPLY+=(-)
            return
            ;;
        -*F)
            COMPREPLY=($(compgen -W "$("$1" -F 2>&1 |
                awk '/^  / { print $1 }')" -- "$cur"))
            return
            ;;
        -*O)
            [[ -v _tshark_protocols ]] ||
                _tshark_protocols="$("$1" -G protocols 2>/dev/null |
                    cut -f 3 | tr '\n' ' ')"
            _comp_delimited , -W "$_tshark_protocols"
            return
            ;;
        -*T)
            # Parse from: tshark -T . 2>&1 | awk -F \" '/^\t*"/ { print $2 }'
            COMPREPLY=($(compgen -W \
                'pdml ps psml json jsonraw ek tabs text fields' -- "$cur"))
            return
            ;;
        -*t)
            # Parse from: tshark -t . 2>&1 | awk -F \" '/^\t*"/ { print $2 }'
            COMPREPLY=($(compgen -W \
                'a ad adoy d dd e r u ud udoy' -- "$cur"))
            return
            ;;
        -*u)
            # TODO: could be parsed from "-u ." output
            COMPREPLY=($(compgen -W 's hms' -- "$cur"))
            return
            ;;
        -*W)
            COMPREPLY=($(compgen -W 'n' -- "$cur"))
            return
            ;;
        -*X)
            if [[ ${cur:${#prefix}} == lua_script:* ]]; then
                cur=${cur#*:}
                _filedir lua
            else
                COMPREPLY=($(compgen -P "$prefix" -W 'lua_script:' -- \
                    "${cur:${#prefix}}"))
                [[ ${COMPREPLY-} == *: ]] && compopt -o nospace
            fi
            return
            ;;
        -*G)
            COMPREPLY=($(compgen -W "$("$1" -G \? 2>/dev/null |
                awk '/^[ \t]*-G / \
                    { sub("^[[]","",$2); sub("[]]$","",$2); print $2 }')" \
                -- "$cur"))
            return
            ;;

    esac

    if [[ $cur == -* ]]; then
        COMPREPLY=($(compgen -W '$(_parse_help "$1" -h 2>/dev/null)' \
            -- "$cur"))
        return
    fi
} &&
    complete -F _tshark tshark

# ex: filetype=sh
